package com.qa.controller;

import com.github.pagehelper.PageInfo;
import com.qa.common.Const;
import com.qa.common.ServerResponse;
import com.qa.pojo.Comment;
import com.qa.pojo.User;
import com.qa.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author
 * @Date 2020/3/9 22:30
 * @Desc
 **/
@Controller
public class CommentController {
    @Autowired
    private ICommentService commentService;

    @GetMapping("/comment/add")
    @ResponseBody
    public ServerResponse commentAdd(@RequestParam("commentId") Integer commentId, @RequestParam("comment") String comment,
                                     HttpServletRequest request, @RequestParam("category") Integer category){
        User user = (User) request.getSession().getAttribute(Const.CURRENT_USER);
        return commentService.addComment(commentId, comment, user, category);
    }


    @GetMapping("/comment/addCategory")
    @ResponseBody
    public ServerResponse commentAddCategory(@RequestParam("comment") String comment,
                                     HttpServletRequest request, @RequestParam("articleId") Integer articleId){
        User user = (User) request.getSession().getAttribute(Const.CURRENT_USER);
            return commentService.addCommentCategory(comment, user, articleId);
    }


    @GetMapping("/cms/comment/page")
    public String commentPage() {
        return "commentList";
    }

    @GetMapping("/cms/comment/list")
    @ResponseBody
    public ServerResponse userList(@RequestParam(value = "comment", required = false) String comment,
                                   @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "limit", defaultValue = "10") Integer pageSize) {
        PageInfo<Comment> pageInfo = commentService.getByComment(comment, pageNum, pageSize);
        return ServerResponse.createBySuccess("查询成功！", pageInfo.getList(), pageInfo.getTotal());
    }

    @GetMapping("/cms/comment/commentDetail")
    public String getCommentDetail(@RequestParam("commentId") Integer commentId, Model model) {
        Comment byCommentId = commentService.getById(commentId);
        model.addAttribute("comment", byCommentId);
        return "commentDetailPage";
    }

    @PostMapping("/cms/comment/update/")
    @ResponseBody
    public ServerResponse commentUpdateStatus(@RequestBody Comment comment) {
        return commentService.updateStatus(comment);
    }

}
